# -*- coding: utf-8 -*-
"""
Created on Sat Mar 27 20:13:00 2021

@author: Apple
"""


import sys
from PyQt5.QtWidgets import (QMainWindow,QWidget,QTableWidget,QHBoxLayout,QMessageBox,QApplication,QTableWidgetItem,QHeaderView)
from exceldemo1 import Ui_MainWindow

class MainWindow(QMainWindow):
    def __init__(self,parent=None):
        super(MainWindow,self).__init__(parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.tableWidget.setRowCount(1)
        self.ui.tableWidget.setColumnCount(3)
        self.ui.tableWidget.setHorizontalHeaderLabels(['姓名','性别','体重'])
        self.ui.tableWidget.resizeColumnsToContents()
        self.ui.tableWidget.resizeRowsToContents()
        newItem = QTableWidgetItem('张三')
        self.ui.tableWidget.setItem(0,0,newItem)
        
        newItem = QTableWidgetItem('男')
        self.ui.tableWidget.setItem(0,1,newItem)      

        newItem = QTableWidgetItem('160')
        self.ui.tableWidget.setItem(0,2,newItem)
    
    def newline(self):
        tableWidget = self.ui.tableWidget
        num = tableWidget.rowCount()
        print(num)
        tableWidget.setRowCount(num+1)
        
    def delline(self):
        tableWidget = self.ui.tableWidget
        num = tableWidget.rowCount()
        print(num)
        tableWidget.setRowCount(num-1)  
        
    def clearResult(self):
        tableWidget = self.ui.tableWidget
        tableWidget.setRowCount(0) # 设置行数
    
    def savedata(self):
        tableWidget = self.ui.tableWidget
        numrow = tableWidget.rowCount()
        numcol = tableWidget.columnCount()
        with open(r'C:\Users\18757\Desktop\软著\PYQT初学\test.txt','a+') as f:
            f.seek(0)
            f.truncate()
        for i in range(numrow):
            for j in range(numcol):
                data = tableWidget.item(i,j).text()
                with open(r'C:\Users\18757\Desktop\软著\PYQT初学\test.txt','a+') as f:
                    f.write(data+'\n')
        


if __name__=="__main__":
    app = QApplication(sys.argv)
    win = MainWindow()
    win.show()
    sys.exit(app.exec_())